package com.learning;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class LearningSQLiteActivity extends Activity {
    
	private static final String DATABASE_NAME = "myDatabase.db";	
	private Button buttonCreate;
	private Button buttonInsert;
	private Button buttonCount;
	private Button buttonDelete;
	
	SQLiteDatabase db;
	DBHelper helper;
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        
        System.out.println("onCreate!");
        
        db = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        helper = new DBHelper(this, db);
        
        buttonInsert = (Button)findViewById(R.id.insert);
        buttonInsert.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				insert();				
			}			
		});
        
        buttonDelete = (Button)findViewById(R.id.delete);
        buttonDelete.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				delete();				
			}			
		});
        
        buttonCount = (Button)findViewById(R.id.count);
        buttonCount.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				count();				
			}			
		});
    }
    
    private void insert() {
		try {
			EditText nameEditText = (EditText) findViewById(R.id.editTextName);
			if(!nameEditText.getText().toString().contentEquals("")) 
			{
				EditText mailEditText = (EditText) findViewById(R.id.editTextMail);
				EditText phoneEditText = (EditText) findViewById(R.id.editTextPhone);
				helper.insert(nameEditText.getText().toString(), mailEditText.getText().toString(), phoneEditText.getText().toString());
			}
		} 
		catch (Exception e) {
			System.out.println("EXCEPTION: " + e.getMessage());
			e.printStackTrace();
		}
	}
    
    private void count() {
    	helper.count();
	}
    
    private void delete() {
    	EditText nameEditText = (EditText) findViewById(R.id.editTextName);
		String name = nameEditText.getText().toString();
		helper.delete(name);		
	}
}